package com.asia5b.a5mall.middleware.base

import android.app.Application
import android.content.Context
import android.text.TextUtils
import androidx.multidex.MultiDexApplication
import com.asia5b.a5bmall.resource.ResColor
import com.asia5b.a5mall.middleware.core.ActivityLifeController
import com.asia5b.a5mall.middleware.utils.LocalManageUtil
import com.asia5b.a5mall.middleware.utils.SharedPreferencesUtils
import com.asia5b.a5mall.middleware.utils.appLanguageSelect


/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   Application基类
 */
open class BaseApplication : MultiDexApplication() {

    /**
     * 伴生对象
     */
    companion object {

        /**
         * @Name  appContext
         * @Type  android.support.multidex.MultiDexApplication()
         * @Note  应用Application对象（单例）
         */
        lateinit var sApplication: Application
        lateinit var sContext: Context

    }

    /**
     * @Note   Application创建时调用
     * @param  ...
     */
    override fun onCreate() {
        super.onCreate()
        /**初始化Application对象*/
        sApplication = this
        sContext = this
        SharedPreferencesUtils.initSharedPreferences(this)
        ResColor.context=this

        //语言设置
        setAppLanguageSelect()

        /**开始Activity生命周期监听，并启动AutoClearHelper*/
        registerActivityLifecycleCallbacks(ActivityLifeController().withAutoValueHelper())

    }

    /**
     * 初始化语言设置
     */
    private fun setAppLanguageSelect() {
        LocalManageUtil.init(this)
        //手机语言
        if (appLanguageSelect == -1) {//没有设置过语言
            val sysLocale = LocalManageUtil.getSysLocale()
            if (sysLocale != null && sysLocale.language != null) {
                val language = sysLocale!!.language!!
                if (TextUtils.equals(language, "en")) {
                    appLanguageSelect = LocalManageUtil.LANGUAGE_EN
                } else if (TextUtils.equals(language, "su")) {
                    appLanguageSelect = LocalManageUtil.LANGUAGE_SU
                } else if (TextUtils.equals(language, "ms")) {
                    appLanguageSelect = LocalManageUtil.LANGUAGE_MS
                } else if (TextUtils.equals(language, "th")) {
                    appLanguageSelect = LocalManageUtil.LANGUAGE_TH
                } else if (TextUtils.equals(language, "vi")) {
                    appLanguageSelect = LocalManageUtil.LANGUAGE_VI
                } else {
                    //默认设置中文
                    appLanguageSelect = LocalManageUtil.LANGUAGE_CN
                }
            } else {
                //默认设置中文
                appLanguageSelect = LocalManageUtil.LANGUAGE_CN
            }
            LocalManageUtil.setAppLanguageSelect(appLanguageSelect)
        } else {
            LocalManageUtil.setAppLanguageSelect(appLanguageSelect)
        }

    }

}